<?php
namespace App\Http\Controllers\Wxxcx;

use App\Services\Wxxcx\TmpService;
use App\Utilitys\Upload;
use Illuminate\Http\Request;

class TmpController extends BaseController
{
    /**
     * 首页模板
     */

    public function __construct(TmpService $service)
    {
        $this->db = $service;
    }

    /**
     * 场景选项
     */
    public function sceneSel(Request $request)
    {
        $res = $this->db->sceneSel();
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    /**
     * 场景选项
     */
    public function sceneList(Request $request)
    {
        $res = $this->db->sceneList();
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    /**
     * 列表
     */
    public function getList(Request $request)
    {
        $pages['start'] = $request->post('start', 1);
        $pages['limit'] = $request->post('limit', 10);
        $pas['uid'] = $request->post('uid', 0);
        $pas['scene_id'] = $request->post('scene_id', 0);
        if (!$pas['uid']) {
            return $this->toJson(-2, '必须登录');
        }
        $res = $this->db->getList($pages, $pas);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    /**
     * 是否存在
     */
    public function exist(Request $request)
    {
        $code = $request->post('code', '');
        $res = $this->db->exist($code);
        if (!empty($res)) {
            return $this->toJson(-2, '已被使用');
        }
        return $this->toJson();
    }

    /**
     * 添加
     */
    public function add(Request $request)
    {
        $pas['uid'] = $request->post('uid', 0);
        $pas['code'] = $request->post('code', '');
        $pas['title'] = $request->post('title', '');
        $pas['scene_id'] = $request->post('scene_id', 0);
        $pas['describe'] = $request->post('describe', '');
        $pas['thumb'] = $request->post('thumb', '');
        if (!$pas['uid']) {
            return $this->toJson(-2, '没有登录');
        }
        if (!$pas['code']) {
            return $this->toJson(-2, '没有码');
        }
        $res = $this->db->getQrByCode($pas['code']);
        if ($res['code'] != 0) {
            return $this->toJson(-2, $res['msg']);
        }
        if (!$pas['title']) {
            return $this->toJson(-2, '没有物品名');
        }
        if ($pas['thumb']) {
            $pas['thumb'] = Upload::getPathByBase64($pas['thumb']);
        }
        $res = $this->db->add($pas);
        if ($res['code'] != 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    /**
     * 详情
     */
    public function getOne(Request $request)
    {
        $code = $request->post('code', '');
        $res = $this->db->getOne($code);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    /**
     * 上传封面图
     */
    public function uploadThumb(Request $request)
    {
        $id = $request->post('id', 0);
        $base64= $request->post('base64', '');
        if (!$id || !$base64) {
            return $this->toJson(-2, '参数错误');
        }
        $thumb = Upload::getPathByBase64($base64);
        $res = $this->db->uploadThumb($id, $thumb);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    /**
     * 修改
     */
    public function modify(Request $request)
    {
        $uid = $request->post('uid', 0);
        $id = $request->post('id', 0);
        $pas['title'] = $request->post('title', '');
        $pas['describe'] = $request->post('describe', '');
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        if (!$id) {
            return $this->toJson(-2, '参数错误');
        }
        $res = $this->db->modify($id, $pas);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    /**
     * 属性修改
     */
    public function attrModify(Request $request)
    {
        $pas['tmp_id'] = $request->post('id', 0);
        $attrid = $request->post('attr_id', 0);
        $pas['attr_name'] = $request->post('attr_name', '');
        $pas['val'] = $request->post('attr_val', '');
        if (!$pas['tmp_id']) {
            return $this->toJson(-2, '参数错误');
        }
        $res = $this->db->attrModify($attrid, $pas);
        if (!$res) {
            return $this->toJson(-2, '更新失败');
        }
        return $this->toJson(0);
    }

    /**
     * 属性删除
     */
    public function attrDel(Request $request)
    {
        $attrid = $request->post('attr_id', 0);
        if (!$attrid) {
            return $this->toJson(-2, '参数错误');
        }
        $res = $this->db->attrDel($attrid);
        if (!$res) {
            return $this->toJson(-2, '删除失败');
        }
        return $this->toJson(0);
    }
}